﻿// See https://aka.ms/new-console-template for more information

//Console.WriteLine("Hello, World!");

namespace ConsoleApp1
{
    internal class Test01
    {
        
        //找到全部因数
        public static void FindFactor(int n,ref List<int> factors)
        {
            for (int i = 1; i <= n; i++ ){
                if (n % i == 0)
                {
                    factors.Add(i);
                }
            }
        }

        //判断是否为质数
        public static bool IsPrime(int n)
        {
            if (n == 1) return false;
            for (int i = 2; i <= Math.Sqrt(n); i++)
            {
                if (n % i == 0) return false;
            }

            return true;
        }

        //确定质因子
        public static void FindPrime(List<int> factors, ref List<int> primes)
        {
            if(factors.Count==0) return;
            foreach (var item in factors)
            {
                if (IsPrime(item))
                {
                    primes.Add(item);
                }
            }
            return;
        }

        static void Main(string[] args)
        {
            List<int> factors = new List<int>();
            List<int> primes = new List<int>();
            Console.WriteLine("请输入一个自然数");
            int n = int.Parse(Console.ReadLine());
            if (n <= 0)
            {
                Console.WriteLine("请重新输入一个符合要求的数");
            }
            else
            {
                FindFactor(n, ref factors);
                FindPrime(factors, ref primes);
                if (primes.Count > 0)
                {   
                    Console.WriteLine("该数的因数有：");
                    foreach (var item in factors)
                    {
                        Console.Write(item + " ");
                    }
                    Console.WriteLine();
                    Console.WriteLine("该数的质因子有：");
                    foreach (var item in primes)
                    {
                        Console.Write(item + " ");
                    }
                }
                else
                {
                    Console.WriteLine("没有找到质因子。");
                }
            }
        }
    }
}


